#ifndef _Cirule_Buffer_h__
#define _Cirule_Buffer_h__

#include <stdint.h>
#include "os/types.h"

typedef struct {
	u8 * buffer;
	int32_t size;
	int32_t w_pos;
	int32_t r_pos;
}c_buffer_t;

//used by dma
static __inline__ void circle_update_write_position(c_buffer_t *cbuff, int32_t pos){
	cbuff->w_pos = pos;
}

static __inline__ int32_t circle_get_read_position(c_buffer_t *cbuff){
	return cbuff->r_pos;
}

static __inline__ int32_t circle_get_write_space(c_buffer_t *cbuff){
	int32_t size = (cbuff->w_pos >= cbuff->r_pos)?(cbuff->size - cbuff->w_pos + cbuff->r_pos):(cbuff->r_pos - cbuff->w_pos);
	size -= 1;
	if (size <= 0){
		return 0;
	}
	return size;
}

static __inline__ int32_t circle_get_read_space(c_buffer_t *cbuff){
	int32_t size = (cbuff->r_pos > cbuff->w_pos) ? (cbuff->size - cbuff->r_pos + cbuff->w_pos) : (cbuff->w_pos - cbuff->r_pos);
	if (size <= 0){
		return 0;
	}
	return size;
}

c_buffer_t *circle_buffer_init(c_buffer_t *cbuff, u8 *buffer, int32_t max_len);
void circle_reset(c_buffer_t *cbuff);
int32_t circle_put_byte(c_buffer_t *cbuff, u8 data);
int32_t circle_put_bytes(c_buffer_t *cbuff, u8 *data, int32_t len);
int32_t circle_get_byte(c_buffer_t *cbuff, u8 *data);
int32_t circle_get_bytes(c_buffer_t *cbuffer, u8 *data, int32_t len);
u32 circle_get_read_size(c_buffer_t *cbuff);
u32 circle_get_read_head(c_buffer_t *cbuff);
void circle_skip_read_size(c_buffer_t *cbuff, int32_t size);

#endif /* _Cirule_Buffer_h__ */

